﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Collections.Generic;

namespace Tesla.Content {
    internal sealed class ResourceLoaderCollection {
        private Dictionary<String, List<ResourceLoader>> _loaders;
        private ContentManager _contentMan;

        public ResourceLoader this[String extension, Type targetType] {
            get {
                List<ResourceLoader> list;
                if(_loaders.TryGetValue(extension, out list)) {
                    return FindLoader(list, targetType);
                }
                return null;
            }
        }

        public ResourceLoaderCollection(ContentManager manager) {
            _loaders = new Dictionary<String, List<ResourceLoader>>();
            _contentMan = manager;
        }

        public bool Add(ResourceLoader loader) {
            if(loader == null) {
                return false;
            }

            List<ResourceLoader> list;
            bool createdList = false;
            if(!_loaders.TryGetValue(loader.Extension, out list)) {
                list = new List<ResourceLoader>();
                _loaders.Add(loader.Extension, list);
                createdList = true;
            }

            if(!createdList) {
                Remove(loader.Extension, loader.TargetType);
            }

            list.Add(loader);
            loader.Initialize(_contentMan);
            return true;
        }

        public ResourceLoader Remove(String extension, Type targetType) {
            List<ResourceLoader> list;
            if(_loaders.TryGetValue(extension, out list)) {
                ResourceLoader loader = FindLoader(list, targetType);
                if(loader != null) {
                    list.Remove(loader);
                    loader.Initialize(null);
                    return loader;
                }
            }
            return null;
        }

        public void Clear() {
            foreach(KeyValuePair<String, List<ResourceLoader>> kv in _loaders) {
                List<ResourceLoader> list = kv.Value;
                for(int i = 0; i < list.Count; i++) {
                    ResourceLoader loader = list[i];
                    loader.Initialize(null);
                }
            }
            _loaders.Clear();
        }

        public void ClearByExtension(String extension) {
            List<ResourceLoader> list;
            if(_loaders.TryGetValue(extension, out list)) {
                for(int i = 0; i < list.Count; i++) {
                    ResourceLoader loader = list[i];
                    loader.Initialize(null);
                }
            }
        }

        public void ClearByType(Type targetType) {
            foreach(KeyValuePair<String, List<ResourceLoader>> kv in _loaders) {
                List<ResourceLoader> list = kv.Value;
                ResourceLoader loader = FindLoader(list, targetType);
                if(loader != null) {
                    list.Remove(loader);
                    loader.Initialize(null);
                }
            }
        }

        public bool Contains(ResourceLoader loader) {
            return Contains(loader.Extension, loader.TargetType);
        }

        public bool Contains(String extension, Type targetType) {
            List<ResourceLoader> list;
            if(_loaders.TryGetValue(extension, out list)) {
                return FindLoader(list, targetType) != null;
            }
            return false;
        }

        private ResourceLoader FindLoader(List<ResourceLoader> list, Type targetType) {
            for(int i = 0; i < list.Count; i++) {
                ResourceLoader loader = list[i];
                Type loaderTargetType = loader.TargetType;
                if(loaderTargetType.Equals(targetType) || loaderTargetType.IsAssignableFrom(targetType)) {
                    return loader;
                }
            }
            return null;
        }
    }
}
